<?php
namespace app\api\controller;

/**
 * 需要登录情景下使用的基础类
 */
class AuthBase extends ApiBase{
	public $accessToken = '';
	public $userId = 0;
	public $username = '';

	/*中间件处理登录
	public function handle($request,\Closure $next){echo 11;exit;
		if(!$this->isLogin()){
			return show(config('status.not_login'),'未登录');
		}
		$response = $next($request);
		return $response;
	}*/

	public function initialize(){
		parent::initialize();
		$this->accessToken = $this->request->header('access_token');

		if(!$this->isLogin() || !$this->accessToken){
			return show(config('status.not_login'),'未登录');
		}
	}

	/** 
	 * 判断是否登录
	 */
	public function isLogin(){
		$accessToken = $this->accessToken;
		if(empty($accessToken)){
			return false;
		}else{
			$userInfo = cache(config('redis.token_pre').$this->accessToken);
			if(empty($userInfo)){
				return false;
			}
			if(!empty($userinfo['id']) && !empty($userinfo['username'])){
				$this->username = $userinfo['username'];
				$this->userId = $userinfo['id'];
				return true;
			}
		}
	}
}